package model;

import java.sql.ResultSet;
import java.sql.SQLException;
import java.text.SimpleDateFormat;
import java.util.Date;

public class PaperLog extends Paper{
	protected int uid = 0;
	protected String username = null;
	protected int score = 0;
	protected long dateline = -1;
	
	public PaperLog(Paper p){
		this.setPid(p.getPid());
		this.setTitle(p.getTitle());
		this.setAuthorId(p.getAuthorId());
		this.setQuestionNum(p.getQuestionNum());
		this.setTimeout(p.getTimeout());
		this.setTotalScore(p.getTotalScore());
	}
	
	public int getUid() {
		return uid;
	}
	
	public void setUid(int uid) {
		this.uid = uid;
	}
	
	public String getUsername() {
		return username;
	}
	
	public void setUsername(String username) {
		this.username = username;
	}
	
	public int getScore(){
		return this.score;
	}
	
	public void setScore(int score){
		this.score = score;
	}
	
	public long getDateline() {
		return dateline;
	}
	
	private static SimpleDateFormat date_format = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
	public String getDatelineString(){
		return date_format.format(new Date(this.dateline * 1000));
	}
	
	public void setDateline(long dateline) {
		this.dateline = dateline;
	}
	
	private static final int offset = 6;
	public static PaperLog fromDatabase(ResultSet rs){
		PaperLog l = new PaperLog(Paper.fromDatabase(rs));

		try{
			l.setUid(rs.getInt(offset + 1));
			l.setUsername(rs.getString(offset + 2));
			l.setScore(rs.getInt(offset + 3));
			l.setDateline(rs.getLong(offset + 4));
		}catch(SQLException e){
			e.printStackTrace();
		}
		
		return l;
	}
}
